/******************************************************************************* * Copyright © 2008, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * *******************************************************************************/ package org.eclipse.edt.ide.ui.internal.refactoring; import java.util.Arrays; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspaceDescription; import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.edt.ide.core.Logger; import org.eclipse.edt.ide.ui.EDTUIPlugin; import org.eclipse.edt.ide.ui.internal.UINlsStrings; import org.eclipse.edt.ide.ui.internal.editor.EditorUtility; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.actions.GlobalBuildAction; public class RefactoringSaveHelper { private boolean fFilesSaved; public RefactoringSaveHelper() { } public boolean saveEditors(Shell shell) { IEditorPart[] dirtyEditors= EditorUtility.getDirtyEditors(); if (dirtyEditors.length == 0) return true; if (! saveAllDirtyEditors(shell, dirtyEditors)) return false; try { // Save isn't cancelable. IWorkspace workspace= ResourcesPlugin.getWorkspace(); IWorkspaceDescription description= workspace.getDescription(); boolean autoBuild= description.isAutoBuilding(); description.setAutoBuilding(false); workspace.setDescription(description); try { if (!EDTUIPlugin.getActiveWorkbenchWindow().getWorkbench().saveAllEditors(false)) return false; fFilesSaved= true; } finally { description.setAutoBuilding(autoBuild); workspace.setDescription(description); } return true; } catch (CoreException e) { Logger.log(RefactoringSaveHelper.class, UINlsStrings.RefactoringStarter_unexpected_exception, e); return false; } } public void triggerBuild() { if (fFilesSaved && ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding()) { new GlobalBuildAction(EDTUIPlugin.getActiveWorkbenchWindow(), IncrementalProjectBuilder.INCREMENTAL_BUILD).run(); } } private boolean saveAllDirtyEditors(Shell shell, IEditorPart[] dirtyEditors) { ListDialog dialog= new ListDialog(shell); dialog.setTitle(UINlsStrings.RefactoringStarter_save_all_resources); dialog.setAddCancelButton(true); dialog.setLabelProvider(createDialogLabelProvider()); dialog.setMessage(UINlsStrings.RefactoringStarter_must_save); dialog.setContentProvider(new ArrayContentProvider()); dialog.setInput(Arrays.asList(dirtyEditors)); return dialog.open() == Window.OK; } public boolean hasFilesSaved() { return fFilesSaved; } private ILabelProvider createDialogLabelProvider() { return new LabelProvider() { public Image getImage(Object element) { return ((IEditorPart) element).getTitleImage(); } public String getText(Object element) { return ((IEditorPart) element).getTitle(); } }; } }